package net.jarang.support;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.IOUtils;

import freemarker.template.Template;

public class PropertiesUtil {
	private static Map<String, Properties> propertiesMap = new HashMap<String, Properties>();

	public static Properties getProperties(String resourceName) {
		Properties properties = propertiesMap.get(resourceName);
		if(properties != null) return properties;
		
		InputStream inStream = null;
		try {
			inStream = Template.class.getClassLoader().getResourceAsStream(resourceName);
			properties = new Properties();
			properties.load(inStream);
			propertiesMap.put(resourceName, properties);
		} catch (Exception e) {
			throw new RuntimeException("Cannot load properties " + resourceName, e);
		} finally {
			IOUtils.closeQuietly(inStream);
		}
		return properties;
	}	
}
